﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using utils;

namespace dal.cgm_production_dao
{
    [DataContract]
    public class MacAddressUsed
    {
        [DataMember]
        public int id { get; set; }
        [DataMember]
        public string order { get; set; }
        [DataMember]
        public string model { get; set; }
        [DataMember]
        public int quantity { get; set; }
        [DataMember]
        public string start_mac_address { get; set; }
        [DataMember]
        public string end_mac_address { get; set; }
        [DataMember]
        public string memory_addr { get; set; }
        [DataMember]
        public string chip_name { get; set; }
        [DataMember]
        public DateTime create_time { get; set; }
    }

    public class MacAddressUsedDao
    {
      
        public static void InsertMacAddressUsed(MacAddressUsed data)
        {
            if (data == null)
            {
                return;
            }

            // SQL 语句中将 order 列名用反引号括起来
            string sql = @"INSERT INTO mac_address_used (`order`, model, quantity, start_mac_address,end_mac_address, memory_addr,chip_name, create_time) 
                   VALUES (@order, @model, @quantity, @start_mac_address,@end_mac_address, @memory_addr, @chip_name, @create_time)";
            List<MySqlParameter> parameters = new List<MySqlParameter>();
            parameters.Add(new MySqlParameter("@order", data.order));
            parameters.Add(new MySqlParameter("@model", data.model));
            parameters.Add(new MySqlParameter("@quantity", data.quantity));
            parameters.Add(new MySqlParameter("@start_mac_address", data.start_mac_address));
            parameters.Add(new MySqlParameter("@end_mac_address", data.end_mac_address));
            parameters.Add(new MySqlParameter("@memory_addr", data.memory_addr));
            parameters.Add(new MySqlParameter("@chip_name", data.chip_name));
            parameters.Add(new MySqlParameter("@create_time", data.create_time));
            MySqlHelper.ExecuteNonQuery(ConfigString.cgm_production, sql, parameters.ToArray());

        }

        public static IList<MacAddressUsed> GetMacAddressUsed(string productType, string orderNumber)
        {
            // 基础查询
            string sql = @"SELECT `order`,model,start_mac_address,end_mac_address,quantity, memory_addr,chip_name, create_time FROM mac_address_used   WHERE 1=1";
            // 动态添加条件
            List<MySqlParameter> parameters = new List<MySqlParameter>();

            //if (!string.IsNullOrEmpty(mac))
            //{
            //    sql += " AND mac_address = @mac";
            //    parameters.Add(new MySqlParameter("@mac", mac));
            //}

            if (!string.IsNullOrEmpty(productType))
            {
                sql += " AND model = @productType";
                parameters.Add(new MySqlParameter("@productType", productType));
            }

            if (!string.IsNullOrEmpty(orderNumber))
            {
                sql += " AND `order` = @orderNumber";
                parameters.Add(new MySqlParameter("@orderNumber", orderNumber));
            }

            // 执行查询
            DataSet ds = MySqlHelper.ExecuteDataset(ConfigString.cgm_production, sql, parameters.ToArray());

            IList<MacAddressUsed> list = DataContainerUtil.DataTableToList<MacAddressUsed>(ds.Tables[0]);

            return list.Any() ? list : new List<MacAddressUsed>();
        }

    }
}
